package com.wanxi.education.controller.upload;


import com.wanxi.education.common.CommonResult;
import com.wanxi.education.service.upload.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: WangRui
 * @Description:
 * @Date: Created in 2021/9/3 0003
 * @Version: 1.0
 */
@RestController
@RequestMapping("/upload")
@Api(tags = "上传视频（需要登录）")
public class UploadController {

    @Value("${domainName}")
    private String domain;


    @Autowired
    private UploadService uploadService;

    @PostMapping(value = "/videoFiles", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "视频文件上传接口", notes = "文件上传api必须用表单方式提交")
    public CommonResult<String> videoFiles(@ApiParam(value = "files", required = true)
                                           @RequestParam("files") MultipartFile[] files) {
        String spaceFile = "video";
        String originalFilename = files[0].getOriginalFilename();
        String substring = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        if (substring.equals("mp4")) {
            return getStringCommonResult(files, spaceFile);
        }
        return CommonResult.failure("上传失败，视频格式为" + substring + "，上传的视频格式应该为mp4");
    }

    @PostMapping(value = "/imageFiles", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "图片文件上传接口", notes = "文件上传api必须用表单方式提交")
    public CommonResult<String> imageFiles(@ApiParam(value = "files", required = true)
                                           @RequestParam("files") MultipartFile[] files) {
        String spaceFile = "image";
        return getStringCommonResult(files, spaceFile);
    }

    private CommonResult<String> getStringCommonResult(MultipartFile[] files, String spaceFile) {
        String url = "";
        if (files.length == 1) {
            if (files[0].getSize() > 1024 * 1024 * 1024) {
                return CommonResult.failure("单个视频不能大于1G！");
            } else {
                try {
//                    url = domain +"/"+ uploadService.uploadFile(files[0], spaceFile);
                    url = uploadService.uploadFile(files[0], spaceFile);
                    //return new ResultData<String>().success(url);

                } catch (Exception e) {
                    return CommonResult.failure("视频大于5M，请重新上传！");
                }
            }
        } else {
            for (MultipartFile file : files) {
                if (ObjectUtils.isEmpty(file)) {
                    return CommonResult.failure("文件为空请重新上传!");
                } else if (file.getSize() > 1024 * 1024 * 1024) {
                    return CommonResult.failure("单个视频不能大于1G！");
                } else {
                    try {
//                        url += domain +"/"+ uploadService.uploadFile(file, spaceFile) + " ";
                        url += uploadService.uploadFile(file, spaceFile) + " ";
                        System.out.println("回调地址：" + url);
                    } catch (Exception e) {
                        return CommonResult.failure("视频大于1G，请重新上传！");
                    }
                }
            }
        }
        return CommonResult.success(url);
    }
}
